Skip to content

Renderer transformation refactor - #165

Merged
PringlesGang merged 88 commits into
masterfrom
renderer-transformation-refactor
Jun 24, 2025
Merged

Renderer transformation refactor#165
PringlesGang merged 88 commits into
masterfrom
renderer-transformation-refactor

Conversation

@PringlesGang

@PringlesGang PringlesGang commented Apr 27, 2025

Copy link
Copy Markdown
Member

A refactor of the renderer to make more use of generic transformation matrices instead of a myriad purpose-built overloads.
Also adds useful functions for generating these transformation matrices, monadically transforming RectFs and CornerQuads, and reduces code duplication within and across the renderer implementations.
A number of internal renderer components have also been overhauled to become managed and make use of more modern C++.

Implements DrawMaskedSprite for both DirectX9 and Vulkan.

Should (obviously) not visually change anything for the worse.

@PringlesGang PringlesGang self-assigned this Apr 27, 2025
@PringlesGang PringlesGang added this to the 0.6.X milestone Apr 27, 2025
Comment thread src/renderer/renderer.h
Comment thread src/util.cpp Outdated
Comment thread profiles/common/scriptvars.lua Outdated
@PringlesGang
PringlesGang force-pushed the renderer-transformation-refactor branch from 494045a to e357f84 Compare May 3, 2025 17:59
@PringlesGang
PringlesGang marked this pull request as ready for review May 7, 2025 23:00
@PringlesGang

Copy link
Copy Markdown
Member Author

Best of luck Dext; you're gonna need it o7

Comment thread src/background2d.cpp Outdated
Comment thread src/character2d.cpp Outdated
Comment thread src/character2d.h
std::array<uint16_t, MaxMvlIndices> MvlIndices;
int MvlIndicesCount;
std::vector<VertexBufferSprites> MvlVertices;
std::vector<uint16_t> MvlIndices;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested if there's any perf hit to using vector instead of array here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is, going from ~200fps to ~80fps in debug mode with three characters on screen, but frankly we should be doing these transformations in the vertex shader anyway...
Lemme know if I should look into that in this PR already or if this can wait until the full-scale refactor

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should leave it as an array for now then and move the transformation into the vertex shader later

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait I think I misunderstood your comment. I think most of the performance hit came from the matrix multiplication; I doubt the switch to vector really did anything noticable. Could profile a little more closely later

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay, you should also probably do profiling in optimized build, matrix multiplication will probably be a lot faster there. There's also probably some SIMD that gets done by default too

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah okay, I did some more profiling and it looks to me like the vectorization doesn't really seem to be an impact at all (as in, the loading of the vertices is so fast visual studio can't even process anything meaningful).

I've also checked the matrix multiplications in in release mode, and the fps in the cclcc system menu (which thus far is the worst case scenario I could find) goes from ~800fps on master down to ~340fps, which is a perf hit, but it's still 340fps, and again should be moved to the vertex shader anyway

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we should just move it to the vertex shader. I'd like to see what the numbers are on a worse device like a steam deck though, I assume your computer is probably pretty beefy, so if the perf is a 60% hit on a crappy device it might be a lot worse

Comment thread src/games/cclcc/delusiontrigger.cpp Outdated
NegativeDelusionSprite, glm::vec2{topLeftX, -109.0f},
glm::vec2{424.0f, 557.0f},
{1.0f, 1.0f, 1.0f, (float)spinAlpha / 256.0f}, {1, 1}, spinAngle);
const glm::vec2 topLeft = {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These probably shouldn't be named topLeft or center and more like offset or something, I can't remember if these names were actually accurate or not or something i just put in.

Comment thread src/games/chlcc/delusiontrigger.cpp Outdated
Comment thread src/renderer/dx9/renderer.cpp
Comment thread src/renderer/dx9/renderer.cpp Outdated
Comment thread src/renderer/dx9/renderer.cpp Outdated
Comment thread src/video/ffmpegplayer.cpp
Comment thread src/util.cpp Outdated
Comment thread src/background2d.cpp
@KKhanhH

KKhanhH commented May 10, 2025

Copy link
Copy Markdown
Collaborator

Vulkan seems to be artifacting a lot, it's especially noticable on chlcc, but I'm not sure if it was always like that

@PringlesGang

Copy link
Copy Markdown
Member Author

Vulkan seems to be artifacting a lot, it's especially noticable on chlcc, but I'm not sure if it was always like that

Yeah, Vulkan is very jittery on master too, so I tried not to make things any worse, but also didn't really make an effort to make it much better either. Would need to do a deep dive into learning Vulkan for that at some point

@PringlesGang
PringlesGang force-pushed the renderer-transformation-refactor branch from 7268747 to a10c44a Compare May 12, 2025 22:38
@PringlesGang
PringlesGang force-pushed the renderer-transformation-refactor branch from 75e60b8 to 89de8c2 Compare June 5, 2025 23:28
@newo-2001 newo-2001 mentioned this pull request Jun 11, 2025
Comment thread src/games/cclcc/systemmenu.cpp Outdated
Renderer->DrawVertices(SystemMenuBG.Sheet, SpriteGridVertices,
DisplayGridVertices, 21, 11);
const CornersQuad bgDisp =
CornersQuad(BGDispOffsetTopLeft, BGDispOffsetBottomLeft,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brace initialize instead

Comment thread src/renderer/dx9/renderer.h Outdated
bool isInverted, bool useMaskAlpha) override;

void DrawVertices(const SpriteSheet& sheet,
std::optional<const SpriteSheet> mask,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will always be a copy, for a const object it would make more sense to get a reference somehow, pointer or maybe a typedefed optional<reference_wrapper>?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also just do default constructible spritesheet and validate it using texture id or something

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking it might be better to just make it a pointer instead for consistency with everything else (also the draw text functions are already nullable pointers)

Comment thread src/renderer/opengl/renderer.h Outdated

// ShaderCompiler compiler
ShaderCompiler* Shaders;
std::shared_ptr<ShaderCompiler> Shaders;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lifetime of Shaders isn't unknown or ambiguous, so a shared_ptr isn't necessary here. Scene3D is the only user and it's a member of renderer, so its lifetime will be tied to renderer. Try to avoid shared_ptr unless necessary cuz it does have a perf hit with reference counting + atomic operations.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say just stick to unique_ptr (owner) + ref/raw ptr(users) as a general pattern, but here i think you might be able to just store the value itself and pass references to children

Comment thread src/renderer/opengl/renderer.h Outdated
void QuadSetPosition3DRotated(RectF const& transformedQuad, float depth,
glm::vec2 vanishingPoint, bool stayInScreen,
glm::quat rot, uintptr_t positions, int stride);
std::unique_ptr<SpriteShader> SpriteShaderProgram;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all different types and can just be stored as values

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use optional if late initialization is needed

const GLint TexturesLocation;
};

struct SpriteInvertedUniforms {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this struct necessary

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly, but I thought it useful to give it its own struct anyway in case SpriteInverted ever has to divert from Sprite

Comment thread src/renderer/opengl/shader.h Outdated
struct MaskedSpriteNoAlphaUniforms {
bool operator==(const MaskedSpriteNoAlphaUniforms& other) const = default;

glm::mat4 Projection = glm::mat4();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just do glm::mat4 Projection{}


const size_t indexOffset = IndexBuffer.size();
IndexBuffer.resize(IndexBuffer.size() + indices.size());
std::transform(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can avoid the resize with back_inserter on IndexBuffer.end()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't that continuously push back all the elements, possibly expanding multiple times, instead of resizing just once?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you can reserve in that case for the allocation, using back_inserter lets you avoid the default construction

Comment thread src/renderer/opengl/renderer.h Outdated
static constexpr size_t TextureUnitCount = 15;
static constexpr std::array<GLint, TextureUnitCount> Textures = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
std::array<std::unique_ptr<TextureUnit>, TextureUnitCount> TextureUnits;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be unique_ptrs? You might be able to store TextureUnits or optional TextureUnits instead

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TextureUnits don't have a default constructor, so I needed late initialization. Optional is possible tho

Comment thread src/renderer/opengl/textureunit.h Outdated
void Reserve() { Info->Flushed = false; }

private:
const GLuint Unit;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's meant to be or copyable, maybe don't have member variables as const since those kill the assignment operators

Comment thread src/renderer/renderer.cpp Outdated
// Add outline to the front of the buffers
vertices.insert(vertices.end(), vertices.begin(), vertices.end());

indices.resize(indexCount * 2);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

back_inserter

@PringlesGang
PringlesGang force-pushed the renderer-transformation-refactor branch from a47766e to b6bcc1e Compare June 12, 2025 11:44
@PringlesGang

PringlesGang commented Jun 12, 2025

Copy link
Copy Markdown
Member Author

(Fyi I only rebased, so you'll only have to check the five added commits)

@PringlesGang
PringlesGang requested a review from KKhanhH June 12, 2025 11:48
@PringlesGang
PringlesGang force-pushed the renderer-transformation-refactor branch from b6bcc1e to b6ff901 Compare June 12, 2025 18:58
@PringlesGang
PringlesGang force-pushed the renderer-transformation-refactor branch from 1d16a0e to 27e9e62 Compare June 23, 2025 20:10
@PringlesGang
PringlesGang merged commit 3781706 into master Jun 24, 2025
11 checks passed
@PringlesGang
PringlesGang deleted the renderer-transformation-refactor branch June 24, 2025 21:49
Nergon123 added a commit to Nergon123/impacto that referenced this pull request Jun 28, 2025
Fix code style issues with clang_format

Size and position correction
Proper .gitignore

C;H LCC SystemMenu

Cleanup

almost perfect CHLCC SystemMenu

Added MenuLoopDuration to lua config files

Cleanup

Fixed error after Renderer transformation refactor (pull request CommitteeOfZero#165)

remove comments
Nergon123 added a commit to Nergon123/impacto that referenced this pull request Jun 28, 2025
Fix code style issues with clang_format

Size and position correction
Proper .gitignore

C;H LCC SystemMenu

Cleanup

almost perfect CHLCC SystemMenu

Added MenuLoopDuration to lua config files

Cleanup

Fixed error after Renderer transformation refactor (pull request CommitteeOfZero#165)

remove comments
@Enorovan Enorovan mentioned this pull request Jul 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants